Page({

  /**
   * 页面的初始数据
   */
  data: {
    current:1,
    codeText:'获取验证码',
    counting:false,
    LoginPhone:'',
    LoginPassword:'',
    RegPhone:'',
    Regcode:'',
    RegPassword:'',
    accessToken:''
  },
  // 登陆注册监听
  click(e){
    let index = e.currentTarget.dataset.code;
    this.setData({
      current:index
    })
  },
  //跳转到忘记密码页面
  forgetPassword(){
    wx.navigateTo({
      url: '/pages/login/forgetPassword/index'
    })
  },
  //获取验证码 
 getCode(){
    var that = this;
    // console.log(this.data.RegPhone);
    // console.log( that.data.LoginPhone);
    if (!that.data.counting) {
      //获取验证码
      wx.request({
        url: 'https://api.gowow.cn/user/send/verificationCode',
        data: {
          "phonenumber": that.data.RegPhone
        },
        method:'POST',
        success (res) {
          console.log(res);
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
            })
        }
      }) 
      //开始倒计时60秒
      that.countDown(that, 60);
    } 
 },
 //立即登录或立即注册绑定事件
 sumbitClick(){ 
   let type = this.data.current;
   //注册
   if(type=='0'){
    wx.request({
      url: 'https://api.gowow.cn/user/register',
      data: {
        "phonenumber": this.data.RegPhone,
        "password": this.data.RegPassword,
        "code": this.data.Regcode
      },
      method:'POST',
      success (res) {
        console.log(res);
        if (res.data.code == 0) {
          that.setData({
            current:1
          })
        }
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
   console.log('您点击了注册监听')
   } 
   //登录
  if(type=='1'){
    wx.request({
      url: 'https://api.gowow.cn/user/login',
      data: {
        "password": this.data.LoginPassword,
        "username": this.data.LoginPhone
      },
      method:'POST',
      success (res) {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
        if (res.data.code == 0) {
          var accessToken = `Bearer ` + res.data.data.accessToken
          wx.setStorageSync('token', accessToken);
          console.log("accessToken的值--------- " + accessToken);
          setTimeout(() => {
            wx.reLaunch({
              url: `/pages/home/home?token=${accessToken}`
            })
          }, 700);
        }
      }
    }) 
    console.log('您点击了登录监听') 
  } 
 },
 //监听登陆手机号输入框的值
 inputLoginPhone(event) {
  this.setData({
    LoginPhone: event.detail.value || ''
  });
 },
 //监听登陆密码输入框的值
 inputLoginPassword(event) {
  this.setData({
    LoginPassword: event.detail.value || ''
  });
 },
 //监听注册手机号输入框的值
 inputRegPhone(event) {
  this.setData({
    RegPhone: event.detail.value || ''
  });
  // console.log(this.data.RegPhone);
 },
 //监听注册验证码输入框的值
 inputRegcode(event) {
  this.setData({
    Regcode: event.detail.value || ''
  });
 },
 //监听注册密码输入框的值
 inputRegPassword(event) {
  this.setData({
    RegPassword: event.detail.value || ''
  });
  // console.log(this.data.RegPassword);
 },
  //倒计时60秒
  countDown(that,count){
    if (count == 0) {
      that.setData({
        codeText: '获取验证码',
        counting:false
      })
      return;
    }
    that.setData({
      counting:true,
      codeText: count + '秒后重新获取',
    })
    setTimeout(function(){
      count--;
      that.countDown(that, count);
    }, 1000);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
